{% block content %}
{% if TargetUser %}
	<span class="floatleft">
	    <a href="{% course_url management app='grades' command='manage' %}">{{course}} Grades</a>
		> {{TargetUser.first_name}}&nbsp;{{TargetUser.last_name}}
	</span>
{% endif %}
{% autoescape off %}
<div class="form_container">
    {# Floating Save Button #}
    <form action="." method="post">{% csrf_token %}
		<button type="submit" class="button SaveButton" name="Save" value="Save" style="position: fixed; bottom: 0px; right: 0px; z-index: 5;" title="Save Changes">Save</button>
    
    
    {# Title #}
		<h1>
			<a class="AddButton button floatleft" href="{% course_url management app='grades' command='edit' %}" title="Add a new directly-entered grade">+</a>
			Grades
		</h1>
		<div class="clear"></div>
		{% if Categories and Grades and Metrics %}
			<table class="grade_table left_justify" style="width: 1px;" border="1" cellpadding="2">
				<tr>
					<th></th>
					{% for category_name in CategoryIndex %}
						{% with category=Categories|getitem:category_name %}
							{% with category_info=category.info %}
								{% if category_name != "Total" %}
									<th>
										{% if category_info.link %}
											<a href="{% course_url management app='grades' command='manage' arguments=category_info.link %}">{{category_info.title}}<a>
										{% else %}
											{{category_info.title}}
										{% endif %}
										<br>
										Weight: {{category_info.weight}}
									</th>
								{% endif %}
							{% endwith %}
						{% endwith %}
					{% endfor %}
					<th>Total</th>
				</tr>
				{% for student in UserIndex %}
					{% with grades=Grades|getitem:student %}
						{% with StudentGradingForm=GradingForms|getitem:student %}
							<tr>
								<td width=1>
									{% if grades.link %}
										<a href="{% course_url management app='grades' command='manage' arguments=grades.link %}">{{student.last_name}},&nbsp;{{student.first_name}}</a>
									{% else %}
										{{student.last_name}},&nbsp;{{student.first_name}}
									{% endif %}
								</td>
								{% for category_name in CategoryIndex %}
									{% with assignments=grades|getitem:category_name %}
										{% with category=Categories|getitem:category_name %}
											{% if category.info.grading == "SUM" %}<td>{{assignments.info.points|floatformat:"2"}}%</td>{% endif %}
											{% if category.info.grading == "AVERAGE" %}<td>{{assignments.info.average|floatformat:"2"}}%</td>{% endif %}
											{% if category.info.grading == "WEIGHTED_AVERAGE" %}<td>{{assignments.info.weighted_average|floatformat:"2"}}%</td>{% endif %}
										{% endwith %}
									{% endwith %}
								{% endfor %}
								<td>{{grades.Total|floatformat:"2"}}%</td>
							</tr>
						{% endwith %}
					{% endwith %}
				{% endfor %}
				<tr>
					<td></td>
					{% for category in CategoryIndex %}
						{% with metrics=Metrics|getitem:category %}
							<td>
								<table>
									{% with category=Categories|getitem:category %}
										{% if category.info.grading == "SUM" %}
											<tr><td>Min:</td><td>{{metrics.info.points.min|floatformat:"2"}}%</td></tr>
											<tr><td>Max:</td><td>{{metrics.info.points.max|floatformat:"2"}}%</td></tr>
											<tr><td>Average:</td><td>{{metrics.info.points.average|floatformat:"2"}}%</td></tr>
											<tr><td>Median:</td><td>{{metrics.info.points.median|floatformat:"2"}}%</td></tr>
										{% endif %}
										{% if category.info.grading == "AVERAGE" %}
											<tr><td>Min:</td><td>{{metrics.info.average.min|floatformat:"2"}}%</td></tr>
											<tr><td>Max:</td><td>{{metrics.info.average.max|floatformat:"2"}}%</td></tr>
											<tr><td>Average:</td><td>{{metrics.info.average.average|floatformat:"2"}}%</td></tr>
											<tr><td>Median:</td><td>{{metrics.info.average.median|floatformat:"2"}}%</td></tr>
										{% endif %}
										{% if category.info.grading == "WEIGHTED_AVERAGE" %}
											<tr><td>Min:</td><td>{{metrics.info.weighted_average.min|floatformat:"2"}}%</td></tr>
											<tr><td>Max:</td><td>{{metrics.info.weighted_average.max|floatformat:"2"}}%</td></tr>
											<tr><td>Average:</td><td>{{metrics.info.weighted_average.average|floatformat:"2"}}%</td></tr>
											<tr><td>Median:</td><td>{{metrics.info.weighted_average.median|floatformat:"2"}}%</td></tr>
										{% endif %}
									{% endwith %}
								</table>
							</td>
						{% endwith %}
					{% endfor %}
					<td>
						<table>
							<tr><td>Min:</td><td>{{Metrics.info.min|floatformat:"2"}}%</td></tr>
							<tr><td>Max:</td><td>{{Metrics.info.max|floatformat:"2"}}%</td></tr>
							<tr><td>Average:</td><td>{{Metrics.info.average|floatformat:"2"}}%</td></tr>
							<tr><td>Median:</td><td>{{Metrics.info.median|floatformat:"2"}}%</td></tr>
						</table>
					</td>
				</tr>
			</table>
			<table>
				<tr>
					<td colspan="100%">
						<a class="button" href="{% course_url management app='grades' command='weights' %}" title="Edit grading category information">Edit Categories</a>
						<a class="button" href="{% course_url management app='grades' command='download' %}" title="Download gradebook as .xls">Download Gradebook</a>
					</td>
				</tr>
			</table>
		{% else %}
			<p>There are no grades for this course</p>
		{% endif %}
    </form>
</div>
{% endautoescape %}
{% endblock content %}